
function sendNewEmail(receiver_id, topic, message, callback) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', '/Trabalho_PD/MailWebService', true);
    var request =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '<soap:Body>' +
            '<ns0:sendEmail xmlns:ns0="http://webservice/">' +
            '<receiver_id>' + receiver_id + '</receiver_id>' +
            '<topic>' + topic + '</topic>' +
            '<message>' + message + '</message>' +
            '</ns0:sendEmail>' +
            '</soap:Body>' +
            '</soap:Envelope>';
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4) {
            if (xmlhttp.status === 200) {
                callback($.parseJSON(xmlhttp.responseXML.childNodes.item(0).textContent));
            }
        }
    };

    xmlhttp.onerror = function (e) {
        alert("Error contacting server!");
    };

    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(request);
}

function getEmail(email_id, callback) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', '/Trabalho_PD/MailWebService', true);
    var request =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '<soap:Body>' +
            '<ns0:getEmail xmlns:ns0="http://webservice/">' +
            '<email_id>' + email_id + '</email_id>' +
            '</ns0:getEmail>' +
            '</soap:Body>' +
            '</soap:Envelope>';
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4) {
            if (xmlhttp.status === 200) {
                callback($.parseJSON(xmlhttp.responseXML.childNodes.item(0).textContent));
            }
        }
    };
    
    xmlhttp.onerror = function (e) {
        alert("Error contacting server!");
    };

    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(request);
}

function getEmails(callback) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', '/Trabalho_PD/MailWebService', true);
    var request =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '<soap:Body>' +
            '<ns0:getEmails xmlns:ns0="http://webservice/"/>' +
            '</soap:Body>' +
            '</soap:Envelope>';
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4) {
            if (xmlhttp.status === 200) {
                callback($.parseJSON(xmlhttp.responseXML.childNodes.item(0).textContent));
            }
        }
    };
    
    xmlhttp.onerror = function (e) {
        alert("Error contacting server!");
    };

    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(request);
}


